
Partial Class Parent
    Inherits System.Web.UI.MasterPage

    Private Const landingScript As String = "<script language=""javascript"">" & _
                                                "document.getElementById(""content"").className = ""sectionlanding"";" & _
                                            "</script>"

    Const DEFAULT_UNNAMED_PAGE_TITLE As String = "Untitled Page"
    Const DEFAULT_PAGE_TITLE As String = "Bachelors Program"
    Private objSitemapProvider As SiteMapProvider

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'litPageTitle.Text = ContentBlock1.EkItem.Title

        Dim MetadataList As Ektron.Cms.CustomAttributeList
        Dim Metadata As Ektron.Cms.CustomAttribute
        Dim ContentBlockTitle As String = ""
        Dim ContentBlockID As Integer = 0
        Dim objSitemapProviders As New SiteMapProviderCollection

        If Not Request.QueryString("ekfrm") Is Nothing Then
            Dim FormBlockInfo As New Ektron.Cms.Controls.FormBlock

            FormBlockInfo = FormBlock1
            ContentBlockTitle = HttpUtility.HtmlDecode(FormBlockInfo.EkItem.Title)
            MetadataList = Nothing
            ContentBlockID = FormBlock1.EKItem.ID
        Else
            ContentBlockTitle = HttpUtility.HtmlDecode(ContentBlock1.EkItem.Title)
            MetadataList = ContentBlock1.GetMetaData()
            ContentBlockID = ContentBlock1.EKItem.ID
        End If

        If (Not IsNothing(MetadataList)) AndAlso (Not IsNothing(MetadataList.AttributeList)) Then
            Metadata = MetadataList.GetItemByName("Page SubHeader")
            If (Not IsNothing(Metadata)) Then
                imgPageSubHeader.ImageUrl = Metadata.Value
                If imgPageSubHeader.ImageUrl.Trim.Length > 0 Then
                    imgPageSubHeader.Visible = True
                End If
            End If
        End If

        If (Not Request.QueryString("ekfrm") Is Nothing) And (FormBlock1.EkItem.HyperLink.IndexOf("landing.aspx") > 0) Then
            litLandingStyle.Text = landingScript
        ElseIf ContentBlock1.EkItem.HyperLink.IndexOf("landing.aspx") > 0 Then
            litLandingStyle.Text = landingScript
        Else
            litLandingStyle.Text = ""
        End If

        'Find sitemap provider.
        objSitemapProvider = SiteMap.Providers.Item("bachelorsprogramSitemapProvider")

        'Page Title
        litFakeCrumb.Text = ""
        If String.IsNullOrEmpty(Page.Title) OrElse Page.Title = DEFAULT_UNNAMED_PAGE_TITLE Then
            If objSitemapProvider.CurrentNode Is Nothing Then
                If ContentBlockTitle <> "" Then
                    litFakeCrumb.Text = "<a title='Bachelors Program' href='/bachelorsprogram/default.aspx'>Bachelors Program</a></span><span> > </span><span class='breadread'>" & ContentBlockTitle & "</span>"
                    Page.Title = DEFAULT_PAGE_TITLE & " :: " & ContentBlockTitle
                Else
                    Page.Title = DEFAULT_PAGE_TITLE
                End If

            Else
                Page.Title = GetPageTitleBasedOnSiteNavigation()
                'If ContentBlockTitle <> "" Then
                'Page.Title &= " :: " & ContentBlockTitle
                'End If
            End If
        End If
        If ContentBlockTitle = "" And lblCustomTitle.Text <> "" Then
            lblPageHeading.Text = "<div id='pageheading'><p>" & lblCustomTitle.Text & "</p></div>"
        Else
            lblPageHeading.Text = "<div id='pageheading'><p>" & ContentBlockTitle & "</p></div>"
        End If
        lblPageHeading.Visible = Not imgPageSubHeader.Visible
        lblPageHeading.Visible = False

        ' determine section header
        If Not Request.Url.PathAndQuery().ToLower.Contains("default.aspx") Then
            lblSectionHeader.Text = ""

            'check if information for page
            'Select Case ContentBlockID
            '    Case 2790 : lblSectionHeader.Text = "<div id='sectionheading'><p>" & ContentBlockTitle & "</p></div>"
            '    Case 2792 : lblSectionHeader.Text = "<div id='sectionheading'><p>" & ContentBlockTitle & "</p></div>"
            '    Case 3042 : lblSectionHeader.Text = "<div id='sectionheading'><p>" & ContentBlockTitle & "</p></div>"
            '    Case 2788 : lblSectionHeader.Text = "<div id='sectionheading'><p>" & ContentBlockTitle & "</p></div>"
            'End Select

            If Not objSitemapProvider.CurrentNode Is Nothing Then

                'check if we are on main 2nd level page
                If lblSectionHeader.Text = "" Then
                    If objSitemapProvider.CurrentNode.ParentNode.Url = objSitemapProvider.RootNode.Url Then
                        lblSectionHeader.Text = "<div id='sectionheading'><h2>" & ContentBlockTitle & "</h2></div>"
                    End If
                End If

                'Check to see if we are on 3rd level or lower page
                If lblSectionHeader.Text = "" Then
                    Dim counter As Integer
                    Dim range As Integer
                    range = objSitemapProvider.RootNode.ChildNodes.Count - 1
                    For counter = 0 To range
                        If objSitemapProvider.CurrentNode.IsDescendantOf(objSitemapProvider.RootNode.ChildNodes(counter)) Then
                            lblSectionHeader.Text = "<div id='sectionheading'><h2><a href='" & objSitemapProvider.RootNode.ChildNodes(counter).Url & "'>" & objSitemapProvider.RootNode.ChildNodes(counter).Title & "</a></h2></div>"
                            lblPageHeading.Visible = True
                        End If
                    Next
                End If
            End If
        End If

        Copyright.InnerHtml = Copyright.InnerHtml.Replace("{Year}", CStr(Now.Year))
    End Sub

    Private Function GetPageTitleBasedOnSiteNavigation() As String
        If objSitemapProvider.CurrentNode Is Nothing Then
            Throw New ArgumentException("currentNode cannot be Nothing")
        End If

        'We are visiting a page defined in the site map - build up the
        'page title based on the site map node's place in the hierarchy

        Dim output As String = String.Empty
        Dim currentNode As SiteMapNode = objSitemapProvider.CurrentNode

        While currentNode IsNot Nothing
            If output.Length > 0 Then
                output = currentNode.Title & " :: " & output
            Else
                output = currentNode.Title
            End If

            currentNode = currentNode.ParentNode
        End While

        Return output
    End Function
    
End Class

